package com.util;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;

/**
 * 自定义按钮
 * 
 * @author Administrator
 * 
 */
public class Buttons {
	public Rect rect_btn;
	public boolean is_clicked = false;
	private Paint btn_paint = new Paint();
	protected int x, y;
	private int select = 0;

	private Bitmap[] imgs;
	private Bitmap[] levelImgs;

	public Buttons(Bitmap[] bitmapClicked, Bitmap[] level, int _x, int _y,
			int select) {
		x = _x;
		y = _y;
		this.select = select;
		imgs = bitmapClicked;
		levelImgs = level;
		rect_btn = new Rect(_x, _y, _x + bitmapClicked[select].getWidth(), _y
				+ bitmapClicked[select].getHeight());

		btn_paint.setStyle(Style.STROKE);
		btn_paint.setColor(0xffff0000);

	}

	/**
	 * 是否被按下
	 * 
	 * @param _touchx
	 * @param _touchy
	 * @return
	 */
	public boolean isClicked(int _touchx, int _touchy) {
		if (rect_btn.contains(_touchx, _touchy)) {
			is_clicked = true;
			return true;
		}
		return false;
	}

	/**
	 * 是否被按下
	 * 
	 * @param _touchx
	 * @param _touchy
	 * @return
	 */
	public boolean isClicked1(int _touchx, int _touchy) {
		if (rect_btn.contains(_touchx, _touchy)) {
			return true;
		}
		return false;
	}

	public void setClickOff() {
		is_clicked = false;
	}

	public void setClickOn() {
		is_clicked = true;
	}

	/**
	 * 画按钮
	 * 
	 * @param canvas
	 */
	public void draw(Canvas canvas) {
		if (imgs[select] != null) {
			canvas.drawBitmap(imgs[select], x, y, btn_paint);
			canvas.drawBitmap(
					levelImgs[select],
					x
							+ ((imgs[select].getWidth() - levelImgs[select]
									.getWidth()) >> 1),
					y
							+ ((imgs[select].getHeight() - levelImgs[select]
									.getHeight()) >> 1), btn_paint);
		}
	}

	public void setPosition(int _x, int _y) {
		x = _x;
		y = _y;
		rect_btn.left = _x;
		rect_btn.top = _y;
		rect_btn.right = _x + imgs[select].getWidth();
		rect_btn.bottom = _y + imgs[select].getHeight();
	}

	/**
	 * 释放
	 */
	public void recycle() {
		if (imgs != null) {
			imgs = null;
		}
	}

	public int getSelect() {
		return select;
	}

	public void setSelect(int i) {

		this.select = i;

	}

}
